<?php
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );  // disable IE caching
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" ); 
header( "Cache-Control: no-cache, must-revalidate" ); 
header( "Pragma: no-cache" );
header( "Content-type: text/xml");
$xmlOut=simplexml_load_string("<?xml version=\"1.0\" encoding=\"utf-8\"?><rtdata/>");
$db = new PDO('sqlite:sensor.sqlite');
$db->setAttribute(PDO::ATTR_TIMEOUT, 5);
$result = $db->query("SELECT name,sensor from sensors ORDER BY name ASC");
foreach($result as $row){
	$s=$xmlOut->addChild('sensor',$row["name"]);
	$s->addAttribute('address',$row['sensor']);
	$result1 = $db->query("SELECT unit,chConf.channel,chconf.type,label,rtdata.ts,rtdata.value FROM chConf JOIN units on chconf.type=units.type JOIN rtdata on chconf.sensor=rtdata.sensor and chconf.channel=rtdata.channel where chConf.sensor=".$row["sensor"]." order by chConf.channel");
        foreach($result1 as $row1){
		$c=$s->addChild('channel',"");
		$c->addAttribute('label',$row1['label']);
		$c->addAttribute('channel',$row1['channel']);
		$c->addAttribute('type',$row1['type']);
		$c->addAttribute('unit',$row1['unit']);
		$c->addAttribute('ts',$row1['ts']);
		$c->addAttribute('value',$row1['value']);
        }
}


echo $xmlOut->asXML();
// close the database connection
$db = NULL;

?>
